var game = null;

function Game(ctx){
	this.pad = new Pad(240);
	this.level = new Level();
	this.ctx = ctx;
	this.intervalId = -10;
	this.left = false;
	this.isAlive = false;
	this.ball = new Ball();
	
}

function Pad(x){
	this.x = x;
	this.y = 300;
	this.width = 60;
	this.height = 10;
	this.color = "black";
	this.speed = 20;
}

function Ball(color, ctx){
	this.x = 140;
	this.y = 60;
	this.dx = 2;
	this.dy = 2;
	this.speed = 15;
	if(color == null){
		color = 'red';
	}
	this.color = color;
	
	if(ctx == null){
		return;
	}
    this.radgrad = ctx.createRadialGradient(45,45,10,52,50,30);  
	radgrad.addColorStop(0, '#A7D30C');  
	radgrad.addColorStop(0.9, '#019F62');  
	radgrad.addColorStop(1, 'rgba(1,159,98,0)'); 
}

function Level(levelNo){
	if(levelNo == null){
		this.level =1;
		this.brickCols = 11;
		this.brickRows  = 10;
		this.brickHeight = 10;
		this.brickWidth = 40;
		this.bricks = new Array(this.brickCols);
		var padding =3;
		for ( var rows = 0; rows < this.brickCols; rows++) {
			this.bricks[rows] = new Array(this.brickRows);
			for ( var cols = 0; cols < this.brickRows; cols++) {
				this.bricks[rows][cols] = new Brick(rows, cols, this.brickWidth, this.brickHeight, padding);
			}
		}
	}
}

function Brick(x, y, width, heigth, padding, color){
	if(padding == null){
		padding =0;
	}
	// geometry
	this.x = padding + x*(width+ padding);
	this.y = y*(heigth+ padding);
	this.height = heigth;
	this.width = width;
	
	//style
	if(color == null){
		color = "orange";
	}
	this.color = color;
	
	// hitcounts
	this.hitCount = 0;
	this.hitsToBreak = 1;
	
}



function initBricks(){
	var canvas = document.getElementById("bricks");
	var ctx = canvas.getContext('2d');
	if(ctx == null){
		alert("Your browser can't handle this page. Get firefox.");
	} else {
		var lingrad = ctx.createLinearGradient(180,30,100,320);  
		lingrad.addColorStop(0, '#0000FF');  
		lingrad.addColorStop(0.5, '#f0f0f0');
		ctx.fillStyle = lingrad;
		ctx.fillRect(0,0,480, 320);
		
		game = new Game(ctx);
		
		drawPad(game.pad, ctx);
		drawLevel(game.level, ctx);
		drawBall(game.ball, ctx);
		
		$(document).keydown( keyPressedDown );
		$(document).keyup( keyUp );
	}	
}

function keyUp(){
	clearInterval(game.intervalId);
	game.intervalId = -1;
}

function keyPressedDown(event){
	if(event.keyCode == 39){
		game.left = true;
	} else if(event.keyCode == 37){
		game.left = false;
	} else {
		return;
	}
	if(game.isAlive == false){
		game.isAlive = true;
		setInterval(moveBall, game.ball.speed);
	}	
	game.intervalId = setInterval(move, game.pad.speed);
}

function move(){
	if( game.intervalId > 0){
		if(game.left){
			game.pad.x +=2;
		} else {
			game.pad.x -=2;
		}
		drawPad(game.pad, game.ctx);
	} else {
	}
}

function moveBall(){
	game.ball.x += game.ball.dx;
	drawBall( game.ball, game.ctx);
}

function drawBall(ball, ctx){
	ctx.save();
	ctx.beginPath();
	ctx.arc(ball.x, ball.y, 10, 0, Math.PI*2, true);
	ctx.closePath();
	ctx.fillStyle = ball.color;
	ctx.fill();
	ctx.restore();
}
function drawLevel(level, ctx){
	ctx.save();
	for ( var rows = 0; rows < level.bricks.length; rows++) {
		for ( var cols = 0; cols < level.bricks[0].length; cols++) {
			var brick = level.bricks[rows][cols];
			if(brick.hitCount < brick.hitsToBreak){				
				ctx.fillStyle = brick.color;
				ctx.fillRect(brick.x, brick.y, brick.width, brick.height);
			}
		}
	}
	ctx.restore();
}

function drawPad(pad, ctx){
	ctx.save();
	
	ctx.clearRect(pad.x, pad.y - pad.height/2, -10, 10);
	ctx.clearRect(pad.x + game.pad.width, pad.y - pad.height/2, 10, 10);

	ctx.fillStyle = pad.color;
	ctx.lineCap = 'round';
	ctx.strokeStyle = pad.color;  
    ctx.lineWidth = pad.height;  
	ctx.beginPath();  
	ctx.moveTo(pad.x, pad.y);  
	ctx.lineTo(pad.x + pad.width, pad.y);  
	ctx.stroke();  
	ctx.closePath();
	ctx.restore();
}